<template>
  <div class="welcome-container">
    <el-container>
      <el-header>
        <div>
          <img src="../assets/heima.png" alt="">
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" size="mini" @click="logout">退出登录</el-button>
      </el-header>
      <el-container>
        <el-aside :width="width">
          <div @click="arrow" style="background:#ccc;color:#fff;text-align:center;padding:5px;font-size:12px">|||</div>
          <!-- 一级导航菜单 -->
          <el-menu
            :default-active="defaultActive"
            :unique-opened="true"
            class="el-menu-vertical-demo"
            :collapse="collapse"
            active-text-color="#00ff00"
            :collapse-transition="false"
            :router="true"
            background-color="#333744" 
            text-color="#fff"
          >
            <!-- 1级菜单 -->
            <el-submenu v-for="(item1,index) in  menList" :key="index" :index="'/'+item1.path">
              <template slot="title">
                <!-- <i class="el-icon-location"></i> -->
                <i style="margin-right:5px" :class="iconsObj[item1.id]"></i>
                <span>{{item1.authName}}</span>
              </template>
              <!-- 2级菜单 -->
              <el-menu-item-group v-for="(item2,index2) in  item1.children" :key="index2">
                <el-menu-item
                  @click="handleColor(item2.path)"
                  :index="'/'+item2.path"
                >   <!-- 图标 -->
                <i class="el-icon-menu"></i>
                 {{item2.authName}}</el-menu-item>
              </el-menu-item-group>
            </el-submenu>
            <!--           
            <el-menu-item index="2">
              <i class="el-icon-menu"></i>
              <span slot="title">导航二</span>
            </el-menu-item>
           
            <el-menu-item index="4">
              <i class="el-icon-setting"></i>
              <span slot="title">导航四</span>
            </el-menu-item>
            -->
          </el-menu>
          <!-- 导航菜单 -->
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
           
<script>
import { menus_api } from '@/api'
export default {
  data() {
    return {
      //  默认展开导航
      defaultActive: window.sessionStorage.getItem('defaultActive') || 'users',
      menList: [],
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      //  显示菜单导航的宽度
      width: '200px',
      collapse: false
    }
  },
  created() {
    this.getMenu()
  },
  methods: {
    logout() {
      //  实现退出登录
      /*  
            1、删除本地缓存的token
            2、跳转回login
            */
      window.sessionStorage.setItem('token', null)
      this.$router.push('/login')
    },

    /* 导航菜单显示当前高亮 */
    handleColor(key) {
      //console.log(key, 333)
      //   点击导航菜单需要做两件事
      /* 
      1、获取当前点击菜单的path值  ====index
      2、将获取的值做本地缓存
      
      */
      this.defaultActive = key
      window.sessionStorage.setItem('defaultActive', key)
    },
    /* 获取左侧菜单 */
    async getMenu() {
      const { data: res } = await menus_api()
     // console.log(res.data, 777)
      //  获取菜单数据  并赋值给 menList
      this.menList = res.data
    },

    /* 实现导航才看的展开和收起 */
    arrow() {
      this.collapse = !this.collapse
      this.width = this.width == '60px' ? '200px' : '60px'
    }
  }
}
</script>
<style scoped lang='less'>
.welcome-container {
  height: 100%;
  > .el-container {
    height: 100%;
    display: flex;
    flex-direction: column;
    .el-header {
      background-color: #373d41;
      height: 60px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      > div {
        display: flex;
        align-items: center;
        color: #fff;
        font-size: 20px;
        span {
          margin-left: 10px;
        }
      }
      .el-button {
        height: 40px;
      }
    }

    section {
      flex: 1;
      .el-aside {
         

        
      }

      .el-main {
        background-color: #eaedf1;
      }
    }
  }
}
</style>